package com.example.administrator.bezier_nextpage;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by eyesee on 2017/4/4.
 */

public class BitmapReader {
    public static Bitmap readBitmapFromResource(Context context,int resId){
        InputStream is = context.getResources().openRawResource(resId);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, options);
        options.inSampleSize =calculateInSampleSize(options,120);
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inDither = false;
        options.inScaled = false;
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(is,null,options);
    }

    public static Bitmap readBitmapFromResource(Context context,int resId,int reqWidth){
        InputStream is = context.getResources().openRawResource(resId);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is,null,options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth);
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inDither = false;
        options.inScaled = false;
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(is,null,options);
    }

    public static Bitmap readBitmapFromFile(String imagePath){
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inPreferredConfig = Bitmap.Config.ARGB_8888;
        op.inDither = false;
        op.inScaled = false;
        return BitmapFactory.decodeFile(imagePath,op);
    }

    public static Bitmap readBigBitmapFromFile(String imagePath,int reqWidth){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath,options);
        options.inSampleSize = calculateInSampleSize(options,reqWidth);
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inDither = false;
        options.inScaled = false;
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(imagePath,options);
    }

    public static Bitmap readBitmapFromUrl(String imgUrl){
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inPreferredConfig = Bitmap.Config.ARGB_8888;
        op.inDither = false;
        op.inScaled = false;
        Bitmap bitmap = null;
        try {
            URL url = new URL(imgUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(6*1000);
            if (conn.getResponseCode() != 200) throw  new RuntimeException("request filed");
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is,null,op);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth){
        int width = options.outWidth;
        int inSampleSize = 1;
        if (width > reqWidth){
            int widthRatio = Math.round((float)width/(float)reqWidth);
            inSampleSize =widthRatio;
        }
        return inSampleSize;
    }
}
